<?php
class Add extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/group/add'));
		
		$data = array();
		$tpl = 'rbac/group/groupinfo.html';
		
		//获取所有父分类
		$mNode = new Node_model();
		$result = $mNode->groupAll(array('pid' => 0));
		$data['groups'] = array();
		if(is_array($result))
		foreach ($result as $key => $value) {
			$data['groups'][$value['gid']] = $value['name'];
		}
		
		$this->display($data, $tpl);
	}
	public function json() {
		$pid = $this->post('pid', 'int');
		$name = $this->post('name', 'str');
		
		if(!$name) $this->displayJSON(array('status' => -1, 'message' => '请输入分组名称'));
		
		$mNode = new Node_model();
		//检查分组
		$params = array('name' => $name, 'pid' => $pid);
		$result = $mNode->groupInfo($params);
		if($result) $this->displayJSON(array('status' => -3, 'message' => '分组已存在'));
		
		$cols = array('pid' => $pid, 'name' => $name);
		$result = $mNode->createGroup($cols);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '添加分组成功'));
		else $this->displayJSON(array('status' => -4, 'message' => '添加分组失败'));
	}
}